package handlers

import (
	"cargo-hub/cache"
	"cargo-hub/models"
	"cargo-hub/pkj"
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	redis_key := "login_" + username
	errCount, _ := cache.Rdb.Get(cache.Ctx, redis_key).Int()
	if errCount >= 3 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误次数过多禁止登录",
		})
		return
	}
	var login models.User
	err := models.DB.Where("username=?", username).Debug().Find(&login).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "用户不存在",
		})
		return
	}
	if login.ID == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "账号或密码错误",
		})
		return
	}
	if login.Password != pkj.Md5(password) {
		cache.Rdb.Incr(cache.Ctx, redis_key)
		if errCount == 2 {
			cache.Rdb.Set(cache.Ctx, redis_key, 3, time.Hour*2)
			c.JSON(200, gin.H{
				"code": 500,
				"msg":  "密码错误超过三次,请两小时后重试",
			})
			return
		}
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "密码错误",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": login,
		"msg":  "登录成功",
	})
	return
}
func AddRoleNode(c *gin.Context) {
	rid, _ := strconv.Atoi(c.PostForm("rid"))
	nid, _ := strconv.Atoi(c.PostForm("nid"))
	if rid == 0 || nid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.RoleNode{
		Rid: rid,
		Nid: nid,
	}
	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "success",
	})
	return
}

/*
			SELECT
	  *

FROM

	roles
	LEFT JOIN user_roles on user_roles.rid=roles.id
	WHERE user_roles.uid=2
*/
func ListUserRole(c *gin.Context) {
	uid := c.PostForm("uid")
	var li models.UserRole
	models.DB.Table("roles").Joins("LEFT JOIN user_roles on user_roles.rid=roles.id").Where("user_roles.uid=?", uid).First(&li)
	c.JSON(200, gin.H{
		"code": 200,
		"data": li,
		"msg":  "success",
	})
	return
}

/*
SELECT

	*

FROM

	nodes
	LEFT JOIN role_nodes on role_nodes.nid=nodes.id
	WHERE role_nodes.rid=1
*/
func ListRoleNode(c *gin.Context) {
	rid := c.PostForm("rid")
	var list models.RoleNode
	models.DB.Table("nodes").Joins("LEFT JOIN role_nodes on role_nodes.nid=nodes.id").Where("role_nodes.rid=?", rid).First(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "success",
	})
	return
}
func AddWarehouse(c *gin.Context) {
	ware_name := c.PostForm("ware_name")
	ware_addr := c.PostForm("ware_addr")
	ware_types := c.PostForm("ware_types")
	if ware_name == "" || ware_addr == "" || ware_types == "" {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Warehouse{
		WareName:  ware_name,
		WareAddr:  ware_addr,
		WareTypes: ware_types,
	}
	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func AddOrder(c *gin.Context) {
	oder_name := c.PostForm("oder_name")
	order_num, _ := strconv.Atoi(c.PostForm("order_num"))
	wid, _ := strconv.Atoi(c.PostForm("wid"))
	if oder_name == "" || order_num == 0 || wid == 0 {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "字段不能为空",
		})
		return
	}
	data := models.Order{
		OrderName: oder_name,
		OrderNum:  order_num,
		Wid:       wid,
	}
	models.DB.Create(&data).Debug()
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

/*
	SELECT

warehouses.ware_name as WareName,
warehouses.ware_addr as WareAddr,
warehouses.ware_types as WareTypes,
orders.order_name as OrderName,
orders.order_num as OrderNum,
orders.wid as Wid
FROM
warehouses
LEFT JOIN orders ON orders.wid=warehouses.id
*/
func AllList(c *gin.Context) {
	var list []models.WarehouseOrder
	redis_key := "list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("warehouses").Select("warehouses.ware_name as WareName",
			"warehouses.ware_addr as WareAddr",
			"warehouses.ware_types as WareTypes",
			"orders.order_name as OrderName",
			"orders.order_num as OrderNum",
			"orders.wid as Wid").Joins("LEFT JOIN orders ON orders.wid=warehouses.id").Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "列表成功",
	})
	return
}

/*
	SELECT

warehouses.ware_name as WareName,
warehouses.ware_addr as WareAddr,
warehouses.ware_types as WareTypes,
orders.order_name as OrderName,
orders.order_num as OrderNum,
orders.wid as Wid
FROM
warehouses
LEFT JOIN orders ON orders.wid=warehouses.id
WHERE orders.wid=1
*/
func AllDetail(c *gin.Context) {
	id := c.PostForm("id")
	var list []models.WarehouseOrder
	redis_key := "list"
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		models.DB.Table("warehouses").Select("warehouses.ware_name as WareName",
			"warehouses.ware_addr as WareAddr",
			"warehouses.ware_types as WareTypes",
			"orders.order_name as OrderName",
			"orders.order_num as OrderNum",
			"orders.wid as Wid").Joins("LEFT JOIN orders ON orders.wid=warehouses.id").Where("warehouses.id=?", id).Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "详情成功",
	})
	return
}
